﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using MMS.DAO;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

namespace MMS
{
    public partial class UserControlRentList : UserControl
    {
        public UserControlRentList()
        {
            InitializeComponent();
        }

        private void UserControlRentReturnList_Load(object sender, EventArgs e)
        {
            this.Dock = DockStyle.Fill;
            bciGroupByCustomer.Checked = true;
            bciShowNewOnly.Checked = true;
            bsiGroup.Visibility = BarItemVisibility.Never;
            //bsiShow.Visibility = BarItemVisibility.Never;
            gvRentList.OptionsBehavior.Editable = false;
            PopulateData();
        }

        private void PopulateData()
        {
            List<ViewOrderDTO> viewOrderDTOList = new List<ViewOrderDTO>();
            ViewOrderDAO viewOrderDAO = new ViewOrderDAO(Param.ConnectionString.Value);
            if(bciShowAll.Checked)
            {
                viewOrderDTOList = viewOrderDAO.SelectAll();
            }
            else if(bciShowNewOnly.Checked)
            {
                viewOrderDTOList = viewOrderDAO.SelectIncomplete();
            }
            else
            {
                viewOrderDTOList = viewOrderDAO.SelectComplete();
            }
            gcRentList.DataSource = viewOrderDTOList;
            gvRentList_CustomerId.FieldName = "CustomerId";
            gvRentList_CustomerName.FieldName = "CustomerName";
            gvRentList_OrderId.FieldName = "OrderFormId";
            gvRentList_MaterialName.FieldName = "MaterialName";
            gvRentList_Amount.FieldName = "Amount";
            gvRentList_UnitName.FieldName = "UnitName";
            gvRentList_RentDay.FieldName = "RentDay";
            gvRentList_ReturnDay.FieldName = "ReturnDay";
            gvRentList_CustomerName.GroupIndex = 0;
            gvRentList_OrderId.GroupIndex = 1;
            gvRentList.ExpandAllGroups();
            gvRentList.BestFitColumns();
           

        }

        private void barButtonItem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string barButtonItemName = ((BarItem)e.Item).Name;
            if(barButtonItemName.Equals(bbiRent.Name))
            {
                FormRentMaterial f = new FormRentMaterial();
                f.ShowDialog();
                PopulateData();
            }
            else if(barButtonItemName.Equals(bbiReturn.Name))
            {
                string customerId = ((ViewOrderDTO)gvRentList.GetFocusedRow()).CustomerId;
                CustomerDTO c = (new CustomerDAO(Param.ConnectionString.Value)).Select(customerId);
                if(c != null)
                {
                    FormReturnMaterial f = new FormReturnMaterial(c);
                    f.ShowDialog(this);
                    PopulateData();    
                }}
            else if (barButtonItemName.Equals(bbiRefresh.Name))
            {
                PopulateData();
            }
        }

        private void barCheckIteml_CheckedChanged(object sender, ItemClickEventArgs e)
        {
            string barCheckItemName = ((BarItem)e.Item).Name;

            if (barCheckItemName.Equals(bciShowAll.Name) && bciShowAll.Checked)
            {
                bsiShow.Caption = "Hiển thị: Tất cả đơn hàng";
                bciShowOldOnly.Checked = false;
                bciShowNewOnly.Checked = false;
            }
            else if (barCheckItemName.Equals(bciShowNewOnly.Name) && bciShowNewOnly.Checked)
            {
                bsiShow.Caption = "Hiển thị: Đơn hàng chưa hoàn tất";
                bciShowAll.Checked = false;
                bciShowOldOnly.Checked = false;
            }
            else if (barCheckItemName.Equals(bciShowOldOnly.Name) && bciShowOldOnly.Checked)
            {
                bsiShow.Caption = "Hiển thị: Đơn hàng đã hoàn tất";
                bciShowNewOnly.Checked = false;
                bciShowAll.Checked = false;
            }
            else if (barCheckItemName.Equals(bciNoGroup.Name) && bciNoGroup.Checked)
            {
                bciGroupByCustomer.Checked = false;
                bciGroupByOrder.Checked = false;
            }
            else if (barCheckItemName.Equals(bciGroupByOrder.Name) && bciGroupByOrder.Checked)
            {
                bciGroupByCustomer.Checked = false;
                bciNoGroup.Checked = false;
            }
            else if (barCheckItemName.Equals(bciGroupByCustomer.Name) && bciGroupByCustomer.Checked)
            {
                bciGroupByOrder.Checked = false;
                bciNoGroup.Checked = false;
            }
            if (!(bciGroupByCustomer.Checked || bciGroupByOrder.Checked || bciNoGroup.Checked))
            {
                bciGroupByCustomer.Checked = true;
            }
            if (!(bciShowAll.Checked || bciShowNewOnly.Checked || bciShowOldOnly.Checked))
            {
                bciShowNewOnly.Checked = true;
            }
            if(bciShowAll.Checked || bciShowNewOnly.Checked || bciShowOldOnly.Checked)
            {
                PopulateData();
            }

        }

        private void gvRentList_RowStyle(object sender, DevExpress.XtraGrid.Views.Grid.RowStyleEventArgs e)
        {
            if (e.RowHandle >= 0)
            {
                ViewOrderDTO viewOrder = ((ViewOrderDTO)gvRentList.GetRow(e.RowHandle));
                if (viewOrder.TypeId == 1)
                {
                    e.Appearance.BackColor = Color.Salmon;
                    e.Appearance.BackColor2 = Color.SeaShell;
                }
                else
                {
                    e.Appearance.BackColor = Color.PaleGreen;
                    e.Appearance.BackColor2 = Color.SeaShell;
                }
            }
        }

        private void gvRentList_DoubleClick(object sender, EventArgs e)
        {
            GridView view = (GridView)sender;
            Point pt = view.GridControl.PointToClient(Control.MousePosition);
            GridHitInfo info = view.CalcHitInfo(pt);
            if (info.InRow || info.InRowCell)
            {
                string customerId = ((ViewOrderDTO)gvRentList.GetRow(info.RowHandle)).CustomerId;
                CustomerDTO c = (new CustomerDAO(Param.ConnectionString.Value)).Select(customerId);
                FormReturnMaterial f = new FormReturnMaterial(c);
                f.ShowDialog(this);
                PopulateData();
            }
        }
    }
}
